<?php

namespace app\index\controller;

use think\Controller;
use think\facade\Request;
use app\index\validate\Add;
use think\Db;
use app\index\model\User;

class Article extends Controller
{
    public function index()
    {
        $list = User::order('id', 'ycm')->select();
        $data = Db('add_article')->select();
        $this->assign('res', $data);
        return $this->fetch();
    }

    public function add()
    {
        // 接收用户请求
        if (Request::isPost()) {
            $data = Request::post();
            $file = ['thumb' => $_FILES['thumb']['tmp_name']];
            $data = array_merge($data, $file);
            // dump($data);
            // 校验数据
            $validate = new Add();
            if ($validate->check($data) == false) {
                return $this->error($validate->getError());
            }
            $result = User::where('name')->strict(false)->insert($data);
            if ($result) {
                return $this->success('插入成功！', 'index/article/index');
            }
        }
        return $this->fetch();
    }

    public function edit()
    {
        $id = Request::get('id');
        $data = User::where('id', $id)->find();
        $this->assign('res', $data);
        if (Request::isPost()) {
            $data = Request::post();
            dump($data);
            $id = Request::get('id');
            // dump($id);
            $res = User::where('id', $id)->update($data);
            if ($res) {
                return $this->success('修改成功', 'index/article/index');
            } else {
                return $this->error('修改失败');
            }
        }
        return $this->fetch();
    }
    public function del()
    {
        $id = Request::get('id');
        $res = User::where('id=' . $id)->delete();
        if ($res) {
            $this->success('删除数据成功！');
        } else {
            $this->error('删除数据失败！');
        }
    }
}
